﻿using EMoney.IStock.Client.Models.HD;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EMoney.IStock.Client.DataAccess.HD
{
    /// <summary>
    /// 回复信息数据访问类
    /// </summary>
    public class ReplyDal
    {
        /// <summary>
        /// 获取指定回复信息
        /// </summary>
        /// <param name="topicid"></param>
        /// <returns></returns>
        public Reply GetReply(int topicid)
        {
            string sql = "select * from tb_bbs_reply where topicid=@topicid";

            SQLTools tools = new SQLTools();

            tools.AddSqlParameter("topicid", System.Data.SqlDbType.Int, topicid);

            return SQLHelper.GetDataByColumns<Reply>(ConnectionManager.Emfstock, sql, tools.GetSqlParameters());
        }

        /// <summary>
        /// 获取最近问答
        /// </summary>
        /// <param name="channelid"></param>
        /// <returns></returns>
        public List<RecentReply> GetRecentReply(int channelid)
        {
            string sql = @"SELECT r.replyid,r.topicid,r.replytime,0 'SpecificUserID' FROM dbo.tb_bbs_reply r
                        LEFT JOIN dbo.tb_bbs_main m ON r.topicid=m.topicid WHERE m.channelid=@channelid
                        ORDER BY r.replytime DESC";

            SQLTools tools = new SQLTools();

            tools.AddSqlParameter("channelid", System.Data.SqlDbType.Int, channelid);

            return SQLHelper.GetDataByColumnsLst<RecentReply>(ConnectionManager.Emfstock, sql, tools.GetSqlParameters());
        }

        /// <summary>
        /// 更新赞同数量
        /// </summary>
        /// <param name="replyid"></param>
        /// <returns></returns>
        public int UpdateYesCount(int replyid)
        {
            string sql = "update tb_bbs_reply set yes=yes+1 where replyid=@replyid";

            SQLTools tools = new SQLTools();

            tools.AddSqlParameter("replyid", System.Data.SqlDbType.Int, replyid);

            return SQLHelper.ExecuteNonQuery(ConnectionManager.Emfstock, System.Data.CommandType.Text, sql, tools.GetSqlParameters());
        }

        /// <summary>
        /// 更新反对数量
        /// </summary>
        /// <param name="replyid"></param>
        /// <returns></returns>
        public int UpdateNoCount(int replyid)
        {
            string sql = "update tb_bbs_reply set no=no+1 where replyid=@replyid";

            SQLTools tools = new SQLTools();

            tools.AddSqlParameter("replyid", System.Data.SqlDbType.Int, replyid);

            return SQLHelper.ExecuteNonQuery(ConnectionManager.Emfstock, System.Data.CommandType.Text, sql, tools.GetSqlParameters());
        }

        /// <summary>
        /// 删除回复信息
        /// </summary>
        /// <param name="topicid"></param>
        /// <returns></returns>
        public int DeleteReply(int topicid)
        {
            string sql = "delete from tb_bbs_reply where topicid=@topicid";

            SQLTools tools = new SQLTools();

            tools.AddSqlParameter("topicid", System.Data.SqlDbType.Int, topicid);

            return SQLHelper.ExecuteNonQuery(ConnectionManager.Emfstock, System.Data.CommandType.Text, sql, tools.GetSqlParameters());
        }
    }
}
